#ifndef CAMERA_INCLUDED
#define CAMERA_INCLUDED

#include<opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>  
#include<iostream>

#include "headers/CV.hpp"

using namespace cv;
using namespace std;

//extern Mat frame;
//ͼ�����ݱ���������� 
extern volatile int doubleBufferDealingIndex;
extern Mat image[2];
extern volatile int read_flag;
extern volatile int write_flag;

bool init_Camera();

bool close_Camera();

bool get_line_state(int Line_num);

template <class TpMat = Mat>
bool wait_frame(TpMat &cur_frame) {

	static double t_1, t_2 = 0, T;
	t_1 = 0;T = 0;
	while (write_flag == 0){
		//printf("Line2 status %i\n", get_line_state(2));
		if(t_1 != 0)t_2 = (double(long(clock())) / double(long(CLOCKS_PER_SEC))) - t_1;
		t_1 = double(long(clock())) / double(long(CLOCKS_PER_SEC));
		T += t_2;
		if(T > 3.5){
			close_Camera();
			return false;
		}
	};
	write_flag = 0;
	doubleBufferDealingIndex = !doubleBufferDealingIndex;
	image[!doubleBufferDealingIndex].copyTo(cur_frame);
	
	if(cur_frame.cols < 10){
		close_Camera();
		return false;
	}

	return true;
}

#endif //CAMERA_INCLUDED